using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web.UI;
using Wintellect.PowerCollections;
using System.Web.UI.WebControls;
using System.Collections.ObjectModel;
using System.Web.UI.HtmlControls;

namespace SurveyHost.Core.Controls
{
    [ToolboxItem(false)]
    [ValidationProperty("AnswerText")]
    public class SingleCheckBoxQuestion : BaseSurveyQuestion
    {
        private CheckBox checkbox;
        private LiteralControl literal;
        private string checkedValue = "1";
        private string uncheckedValue = string.Empty;
        private const string _helpText = @"This answer type consists of a single check box.<br/>  
The configuration consists of a single pair of values.  The pair represents the checked, and unchecked, value of the checkbox.  <br/>
The two values are separated by a semicolon ( ; ).  The first value is stored in the data if the checkbox is checked, the second value is stored if the checkbox is unchecked.<br/>
An example would be:<br/><center>1;2</center><br/>The default value is 1 for checked, and the empty string for unchecked.  Note that returning the empty string as the answer will cause the second chance validator to ask the user if they wish not to answer.";

        public override string ConfigurationHelpText
        {
            get
            {
                return _helpText;
            }
        }

        static SingleCheckBoxQuestion()
        {
        }

        /// <summary>
        /// The response from the user
        /// </summary>
        /// <value></value>
        /// <remarks>Note to extenders:  AnswerText = null indicates the user refused to answer.
        /// </remarks>
        public override string AnswerText
        {
            get
            {
                return checkbox.Checked ? checkedValue : uncheckedValue;
            }
            set
            {
                if(value.Equals(checkedValue,StringComparison.OrdinalIgnoreCase))
                    checkbox.Checked = true;
                if(value.Equals(uncheckedValue,StringComparison.OrdinalIgnoreCase))
                    checkbox.Checked = false;
            }
        }

        /// <summary>
        /// Called by the base class at the time it is appropriate to parse the configuration string,
        /// The configuration can be stored or used to create controls.
        /// </summary>
        protected override void ParseConfigurationString()
        {
            if(string.IsNullOrEmpty(ConfigurationString))return;
            string[] parts = ConfigurationString.Split(';');
            if(parts.Length < 1) return;
            checkedValue = parts[0] == "\"\"" ? string.Empty : parts[0];
            if(parts.Length < 2) return;
            uncheckedValue = parts[1] == "\"\"" ? string.Empty : parts[1];
        }

        /// <summary>
        /// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering.
        /// </summary>
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            ParseConfigurationString();
            HtmlGenericControl questionText = new HtmlGenericControl(HtmlTextWriterTag.Div.ToString());
            HtmlGenericControl questionAnswer = new HtmlGenericControl(HtmlTextWriterTag.Div.ToString());
            questionText.Attributes.Add(HtmlTextWriterAttribute.Class.ToString(), "singleCheckBoxQuestionText");
            questionAnswer.Attributes.Add(HtmlTextWriterAttribute.Class.ToString(), "singleCheckBoxQuestionAnswer");
            Controls.Add(questionAnswer);
            Controls.Add(questionText);

            literal = new LiteralControl
              {
                  Text =
                      string.Format(
                      QuestionLineFormat, QuestionIndex, QuestionText)
              };

            questionText.Controls.Add(literal);

            checkbox = new CheckBox();
            checkbox.CheckedChanged += new EventHandler(checkbox_CheckedChanged);

            questionAnswer.Controls.Add(checkbox);
            
            HtmlGenericControl clearDiv = new HtmlGenericControl(HtmlTextWriterTag.Div.ToString());
            clearDiv.Attributes.Add(HtmlTextWriterAttribute.Class.ToString(), "clearDiv");
            Controls.Add(clearDiv);
        }

        void checkbox_CheckedChanged(object sender, EventArgs e)
        {
            OnAnswerGiven(new AnswerEventArgs(QuestionId, AnswerText));
        }
    }
}
